<!--
 * @Author: tkp
 * @Date: 2025-02-13 14:35:56
-->
<template>
  <div class="draw_contain">
    <el-form :model="queryForm" label-position="right" :rules="rules" ref="ruleForm" label-width="90px">
      <el-form-item prop="strategyName" label="策略名称:">
        <Input v-model="queryForm.strategyName" size="medium" disabled clearable placeholder="请输入策略名称"></Input>
      </el-form-item>
      <el-form-item prop="systemName" label="所属系统:">
        <Input v-model="queryForm.systemName" size="medium" disabled clearable placeholder="请输入所属系统"></Input>
      </el-form-item>
      <el-form-item prop="copyStrategyId" label="参照策略:">
        <Select size="medium" placeholder="请选择角色" @select="changeCopyStrategy" :clearable="false" v-model="queryForm.copyStrategyId" :data="strategyList" :jsonType="{'value':'strategyId','label':'strategyName'}"></Select>
      </el-form-item>
      <el-form-item prop="menuIds" label="菜单权限:">
        <el-tree class="tree-border" :data="menuList" :check-strictly="true" show-checkbox ref="menu" node-key="menuId" empty-text="暂无数据" :props="defaultProps" :default-checked-keys="queryForm.menuIds">
          <span slot-scope="{ node }">
            <span>{{node.data.menuName}}</span>
          </span>
        </el-tree>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="okPop">确认</el-button>
        <el-button size="medium" @click="closePop">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { strategyMenuQuery, authMenuTreeList, strategyQuery } from '@/utils/api'
export default {
  props: {
    data: {
      type: Object | String,
      required: true
    }
  },
  data() {
    return {
      queryForm: {
        menuIds: [],
        strategyId: '',
        strategyName: '',
        copyStrategyId: '',
        systemId: '',
        systemName: '',
      },
      menuList: [],
      strategyList: [],
      defaultProps: {
        children: "children",
        label: "menuTitle"
      },
      rules: {
        strategyName: [
          { required: true, message: '此项为必填' },
        ],
        systemName: [
          { required: true, message: '此项为必填' },
        ],
      },
    }
  },
  mounted() {
    this.queryForm.strategyId = this.data.strategyId
    this.queryForm.strategyName = this.data.strategyName
    this.queryForm.systemId = this.data.systemId
    this.queryForm.systemName = this.data.systemName
    this.getMenu()
    this.getStrategy()
    this.getStrategyMenu()
  },
  methods: {
    closePop() {
      this.$emit('menuData', '')
    },
    okPop() {
      let checkedKeys = this.$refs.menu.getCheckedKeys()
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys()
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      this.queryForm.menuIds = checkedKeys
      let param = {
        strategyId: this.queryForm.strategyId,
        menuIds: this.queryForm.menuIds,
        systemId: this.queryForm.systemId,
      }
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$emit('menuData', param)
        } else {
          this.messageWarnPop('请填写完整')
          return false;
        }
      });
    },
    changeCopyStrategy(e) {
      let param = {
        systemId: this.queryForm.systemId,
        strategyId: e,
      }
      strategyMenuQuery(param).then(res => {
        let data = res.data || []
        this.queryForm.menuIds = []
        data.forEach(item => {
          this.queryForm.menuIds.push(item.menuId)
        })
        this.$refs.menu.setCheckedNodes(this.queryForm.menuIds)
        this.$nextTick(() => {
          this.$refs.menu.$forceUpdate();
        })
      })
    },
    getStrategyMenu() {
      let param = {
        systemId: this.queryForm.systemId,
        strategyId: this.queryForm.strategyId,
      }
      strategyMenuQuery(param).then(res => {
        let data = res.data || []
        this.queryForm.menuIds = []
        data.forEach(item => {
          this.queryForm.menuIds.push(item.menuId)
        })
      })
    },
    getStrategy() {
      let param = {
        enable: 1,
        systemId: this.queryForm.systemId,
      }
      strategyQuery(param).then(res => {
        this.strategyList = res.data || []
      })
    },
    getMenu() {
      let param = {
        enable: 1,
        systemId: this.queryForm.systemId,
      }
      authMenuTreeList(param).then(res => {
        this.menuList = res.data || []
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.draw_contain {
  padding: 0 20px;
}
</style>
